<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">



  <meta name="google-site-verification" content="k-_TfM33aVJFwi1JD8Y3HUhdvKJQH6zfXGr9IifVEas">














  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/images/favicon-32x32.png?v=5.1.3" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="12345 log4jlog4j1.2.17  级别OFF 关闭所有日志记录FATAL 严重的错误事件将会导致应用程序的退出ERROR 虽然发生错误事件，但仍然不影响系统的继续运行。WARN 会出现潜在错误的情形。INFO 消息在粗粒度级别上突出强调应用程序的运行过程。DEBUG 指出细粒度信息事件对调试应用程序是非常">
<meta property="og:type" content="article">
<meta property="og:title" content="log4j-的使用和配置简介">
<meta property="og:url" content="https://angular2.top/posts/34717/index.html">
<meta property="og:site_name" content="大括号">
<meta property="og:description" content="12345 log4jlog4j1.2.17  级别OFF 关闭所有日志记录FATAL 严重的错误事件将会导致应用程序的退出ERROR 虽然发生错误事件，但仍然不影响系统的继续运行。WARN 会出现潜在错误的情形。INFO 消息在粗粒度级别上突出强调应用程序的运行过程。DEBUG 指出细粒度信息事件对调试应用程序是非常有帮助的ALL 开启所有的日志记录 级别优先级OFF &amp;gt; FATAL &amp;g">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-11-21T03:03:59.217Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="log4j-的使用和配置简介">
<meta name="twitter:description" content="12345 log4jlog4j1.2.17  级别OFF 关闭所有日志记录FATAL 严重的错误事件将会导致应用程序的退出ERROR 虽然发生错误事件，但仍然不影响系统的继续运行。WARN 会出现潜在错误的情形。INFO 消息在粗粒度级别上突出强调应用程序的运行过程。DEBUG 指出细粒度信息事件对调试应用程序是非常有帮助的ALL 开启所有的日志记录 级别优先级OFF &amp;gt; FATAL &amp;g">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://angular2.top/posts/34717/">





  <title>log4j-的使用和配置简介 | 大括号</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8953b3d3866338b66f755c911e6136f4";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">大括号</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">是时候表演真正的技术了</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://angular2.top/posts/34717/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="无可替代">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/touxiang.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="大括号">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">log4j-的使用和配置简介</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-11-15T15:32:50+08:00">
                2017-11-15
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/我是一枚程序员/" itemprop="url" rel="index">
                    <span itemprop="name">我是一枚程序员</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i> 浏览
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>1<br>2<br>3<br>4<br>5</p>
<dependency><br><groupid>log4j</groupid><br><artifactid>log4j</artifactid><br><version>1.2.17</version><br></dependency>

<p><a href="http://blog.466dd.com/posts/21011/#级别" rel="external nofollow noopener noreferrer" target="_blank"></a>级别<br>OFF 关闭所有日志记录<br>FATAL 严重的错误事件将会导致应用程序的退出<br>ERROR 虽然发生错误事件，但仍然不影响系统的继续运行。<br>WARN 会出现潜在错误的情形。<br>INFO 消息在粗粒度级别上突出强调应用程序的运行过程。<br>DEBUG 指出细粒度信息事件对调试应用程序是非常有帮助的<br>ALL 开启所有的日志记录</p>
<p><a href="http://blog.466dd.com/posts/21011/#级别优先级" rel="external nofollow noopener noreferrer" target="_blank"></a>级别优先级<br>OFF &gt; FATAL &gt; ERROR &gt; WARN &gt; INFO &gt; DEBUG &gt; ALL<br><a href="http://blog.466dd.com/posts/21011/#使用" rel="external nofollow noopener noreferrer" target="_blank"></a>使用<br>在pom.xml 中添加依赖<br>在项目的中添加log4j.properties</p>
<p><a href="http://blog.466dd.com/posts/21011/#配置" rel="external nofollow noopener noreferrer" target="_blank"></a>配置<br><a href="http://blog.466dd.com/posts/21011/#log4-rootLogger" rel="external nofollow noopener noreferrer" target="_blank"></a>log4.rootLogger<br>log4j.rootLogger=日志级别，appender1, appender2, ….<br>注意，rootLogger默认是对整个工程生效<br>注意，如果只想对某些包操作，那么：log4j.logger.com.hutu=info, stdout，表示该日志对package com.hutu生效<br>注意，这样做可以区分dev/线上，也可以减小性能影响：if(log.isDebugEnabled()){log.debug();}</p>
<p><a href="http://blog.466dd.com/posts/21011/#基础配置" rel="external nofollow noopener noreferrer" target="_blank"></a>基础配置<br><a href="http://blog.466dd.com/posts/21011/#Appender" rel="external nofollow noopener noreferrer" target="_blank"></a>Appender<br>属性<br>描述</p>
<p>layout<br>Appender 使用 Layout 对象和与之关联的模式来格式化日志信息。</p>
<p>target<br>目的地可以是控制台、文件，或依赖于 appender 的对象</p>
<p>level<br>级别用来控制过滤日志信息</p>
<p>threshold<br>Appender 可脱离于日志级别定义一个阀值级别，Appender 对象会忽略所有级别 低于阀值级别的日志。</p>
<p>filter<br>Filter 对象可在级别基础之上分析日志信息，来决定 Appender 对象是否处理或忽 略一条日志记录。</p>
<p><a href="http://blog.466dd.com/posts/21011/#所有appender" rel="external nofollow noopener noreferrer" target="_blank"></a>所有appender<br><a href="https://logging.apache.org/log4j/2.x/manual/appenders.html" rel="external nofollow noopener noreferrer" target="_blank">官方文档</a></p>
<p><a href="http://blog.466dd.com/posts/21011/#AppenderSkeleton" rel="external nofollow noopener noreferrer" target="_blank"></a>AppenderSkeleton<br><a href="http://blog.466dd.com/posts/21011/#AsyncAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>AsyncAppender<br><a href="http://blog.466dd.com/posts/21011/#ConsoleAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>ConsoleAppender<br>输出到控制台<br>1<br>2<br>3<br>4<br>5<br>6<br>7</p>
<p>log4j.rootLogger = debug , stdout , D , E</p>
<h3 id="输出到控制台"><a href="#输出到控制台" class="headerlink" title="输出到控制台"></a>输出到控制台</h3><p>log4j.appender.stdout = org.apache.log4j.ConsoleAppender<br>log4j.appender.stdout.Target = System.out<br>log4j.appender.stdout.layout = org.apache.log4j.PatternLayout<br>log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n</p>
<p><a href="http://blog.466dd.com/posts/21011/#DailyRollingFileAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>DailyRollingFileAppender<br>每天产生一个文件<br><a href="http://blog.466dd.com/posts/21011/#ExternallyRolledFileAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>ExternallyRolledFileAppender<br><a href="http://blog.466dd.com/posts/21011/#FileAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>FileAppender<br>文件<br><a href="http://blog.466dd.com/posts/21011/#JDBCAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>JDBCAppender<br>将日志信息保存到数据库中<br><a href="http://blog.466dd.com/posts/21011/#JMSAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>JMSAppender<br><a href="http://blog.466dd.com/posts/21011/#LF5Appender" rel="external nofollow noopener noreferrer" target="_blank"></a>LF5Appender<br><a href="http://blog.466dd.com/posts/21011/#NTEventLogAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>NTEventLogAppender<br><a href="http://blog.466dd.com/posts/21011/#NullAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>NullAppender<br><a href="http://blog.466dd.com/posts/21011/#RollingFileAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>RollingFileAppender<br>文件大小到达指定大小,则新创建个文件<br><a href="http://blog.466dd.com/posts/21011/#SMTPAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>SMTPAppender<br><a href="http://blog.466dd.com/posts/21011/#SocketAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>SocketAppender<br><a href="http://blog.466dd.com/posts/21011/#SocketHubAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>SocketHubAppender<br><a href="http://blog.466dd.com/posts/21011/#SyslogAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>SyslogAppender<br><a href="http://blog.466dd.com/posts/21011/#TenetAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>TenetAppender<br><a href="http://blog.466dd.com/posts/21011/#WriterAppender" rel="external nofollow noopener noreferrer" target="_blank"></a>WriterAppender<br>将日志信息以流格式发送到任意指定的地方<br><a href="http://blog.466dd.com/posts/21011/#Layout" rel="external nofollow noopener noreferrer" target="_blank"></a>Layout<br><a href="https://logging.apache.org/log4j/2.x/manual/layouts.html" rel="external nofollow noopener noreferrer" target="_blank">Layouts</a><br><a href="http://blog.466dd.com/posts/21011/#DateLayout" rel="external nofollow noopener noreferrer" target="_blank"></a>DateLayout<br><a href="http://blog.466dd.com/posts/21011/#HTMLLayout" rel="external nofollow noopener noreferrer" target="_blank"></a>HTMLLayout<br><a href="http://blog.466dd.com/posts/21011/#PatternLayout" rel="external nofollow noopener noreferrer" target="_blank"></a>PatternLayout<br><a href="http://blog.466dd.com/posts/21011/#SimpleLayout" rel="external nofollow noopener noreferrer" target="_blank"></a>SimpleLayout<br><a href="http://blog.466dd.com/posts/21011/#XMLLayout" rel="external nofollow noopener noreferrer" target="_blank"></a>XMLLayout<br><a href="http://blog.466dd.com/posts/21011/#日志格式化" rel="external nofollow noopener noreferrer" target="_blank"></a>日志格式化<br><a href="http://blog.466dd.com/posts/21011/#模式转换符" rel="external nofollow noopener noreferrer" target="_blank"></a>模式转换符<br>转换字符<br>含义</p>
<p>c<br>使用它为输出日志分类,比如对于分类”a.b.c”,模式%c{2}会输出”b.c”</p>
<p>C<br>使用它输出发起记录日志请求的类的全名。比如对于类”cn.fondme.logger.XXClass”,模式%C{1}会输出”XXClass”</p>
<p>d<br>使用它输出记录日志的日期,比如%d{HH:mm:ss}</p>
<p>F<br>在记录日志时,使用它输出文件名</p>
<p>I<br>用它输出生成日志的调用者的地域信息。</p>
<p>L<br>使用它输出发起日志请求的行号。</p>
<p>m<br>使用它输出和日志事件关联的，由应用提供的信息。</p>
<p>M<br>使用它输出发起日志请求的方法名。</p>
<p>n<br>输出平台相关的换行符。</p>
<p>p<br>输出日志事件的优先级。</p>
<p>r<br>使用它输出从构建布局到生成日志事件所花费的时间，以毫秒为单位。</p>
<p>t<br>输出生成日志事件的线程名。</p>
<p>x<br>输出和生成日志事件线程相关的 NDC (嵌套诊断上下文)。</p>
<p>X<br>该字符后跟 MDC 键，比如 X{clientIP} 会输出保存在 MDC 中键 clientIP 对应的值。</p>
<p>%<br>百分号， %% 会输出一个 %。</p>
<p><a href="http://blog.466dd.com/posts/21011/#格式修饰符" rel="external nofollow noopener noreferrer" target="_blank"></a>格式修饰符<br>格式修饰符<br>左对齐<br>最小宽度<br>最大宽度<br>注释</p>
<p>%20c<br>否<br>20<br>无<br>如果列名少于 20 个字符，左边使用空格补齐</p>
<p>%-20c<br>是<br>20<br>无<br>如果列名少于 20 个字符，右边使用空格补齐。</p>
<p>%.30c<br>不适用<br>无<br>30<br>如果列名长于 30 个字符，从开头剪除。</p>
<p>%20.30c<br>否<br>20<br>30<br>如果列名少于 20 个字符，左边使用空格补齐，如果列名长于 30 个字 符，从开头剪除。</p>
<p>%-20.30c<br>是<br>20<br>30<br>如果列名少于 20 个字符，右边使用空格补齐，如果列名长于 30 个字 符，从开头剪除。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>文章不错，你都不请我喝杯茶，就是说你呀！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechatpay.png" alt="无可替代 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="无可替代 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/11432/" rel="next" title="maven的pom-xml文件详解">
                <i class="fa fa-chevron-left"></i> maven的pom-xml文件详解
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/62108/" rel="prev" title="HTML-5-Web-存储-sessionStorage和localStorage">
                HTML-5-Web-存储-sessionStorage和localStorage <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/touxiang.jpg" alt="无可替代">
            
              <p class="site-author-name" itemprop="name">无可替代</p>
              <p class="site-description motion-element" itemprop="description">大括号的博客</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">129</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/liepeng328" target="_blank" title="GitHub" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://gitee.com/lovepeng" target="_blank" title="码云" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>码云</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://coding.net/u/dakuohao" target="_blank" title="Coding" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>Coding</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.toutiao.com/c/user/2585432515/#mid=6741594486" target="_blank" title="今日头条" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>今日头条</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.zhihu.com/people/dakuohao/activities" target="_blank" title="知乎" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>知乎</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/4ea0af17fd67" target="_blank" title="简书" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=hOjr8uH04erjt7a8xPX1qufr6Q" target="_blank" title="E-Mail" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>E-Mail</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://jq.qq.com/?_wv=1027&k=5ISEgpc" target="_blank" title="QQ群" rel="external nofollow noopener noreferrer">
                    
                      <i class="fa fa-fw fa-globe"></i>QQ群</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#输出到控制台"><span class="nav-number">1.</span> <span class="nav-text">输出到控制台</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">无可替代</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io" rel="external nofollow noopener noreferrer">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow noopener noreferrer">NexT.Pisces</a> v5.1.3</div>



<span class="post-meta-divider">|</span>
<div class="theme-info">
Hosted by <a class="theme-link" target="_blank" href="https://pages.coding.me" rel="external nofollow noopener noreferrer">Coding Pages</a>
</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i> 访问人数
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i> 访问总量
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
